A conventional electronic device having a spectrum detection function is comprised of a diffraction grating, a focusing lens, and a photosensitive component, and is configured to divide a light to be detected into a number of beams through the diffraction grating. In order to ensure dispersion of a plurality of beams for detection by the photosensitive component, the photosensitive component generally needs to be in a distance away from the diffraction grating. In addition, in order to detect all beams after the plurality of beams are dispersed, there must be a large enough photosensitive area, which is generally larger than the diffraction grating. Thus, a large enough space must be reserved in the existing electronic device having a spectrum detection function for arranging the diffraction grating and the photosensitive component. However, it is not convenient to enable the electronic device to be miniature and portable. Thus, the diffraction grating and the photosensitive component are not suitable for use in devices used by consumers in daily life.